package com.snap.vseries.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.snap.vseries.AppConfig;
import com.snap.vseries.R;
import com.snap.vseries.base.BaseActivity;
import com.snap.vseries.manager.FileManager;
import com.snap.vseries.model.DBManager;
import com.snap.vseries.ui.widget.DialogView;
import com.snap.vseries.utils.ToastUtils;
import com.snap.vseries.utils.TopTitleUtils;

import org.litepal.crud.callback.UpdateOrDeleteCallback;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 模板内容
 */
public class TemplateActivity extends BaseActivity {

    @BindView(R.id.delete_template_tv)
    TextView deleteTemplateTv;
    private String name;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    private void initData() {
        name = getIntent().getStringExtra("name");
    }


    private void initView() {
        TextView textView = new TextView(this);
        textView.setText("编辑");
        textView.setTextSize(16);
        textView.setTextColor(getResources().getColor(R.color.black666));
        textView.setClickable(false);
        TopTitleUtils top = new TopTitleUtils(this).setTitle(name);
        if (AppConfig.Dynamic.custom_setting) {
            deleteTemplateTv.setVisibility(View.VISIBLE);
            top.setTitle(name).addRightView(textView)
                    .setRightViewVisiable(View.VISIBLE)
                    .setRight(v -> {
                        Intent intent = new Intent(TemplateActivity.this, TemplateNewActivity.class);
                        intent.putExtra("name", name);
                        startActivity(intent);
                        finish();
                    });
        }
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_template;
    }


    @OnClick({R.id.tv_start_experiment, R.id.delete_template_tv})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.tv_start_experiment:
                Intent intent = new Intent(this, TemplateStartActivity.class);
                intent.putExtra("name", name);
                startActivity(intent);
                break;
            case R.id.delete_template_tv:
                new DialogView(this).setTitle("删除该项目")
                        .setTextContent("确定删除该项目？")
                        .setOnClickListener(content -> {
                            delete();

                        }).show();
                break;
            default:
                break;
        }
    }

    private void delete() {
        DBManager.get().deleteTemplate(new UpdateOrDeleteCallback() {
            @Override
            public void onFinish(int rowsAffected) {
                if (rowsAffected > 0) {
                    FileManager.getInstance().removeTempData(name);
                    ToastUtils.get().showText("删除项目成功");
                    finish();
                }
            }
        }, name);
    }

}
